package com.bruce.codeFactory.Thread.lock;

import java.util.Optional;
import java.util.stream.Stream;

/**
 * @author luotuan
 * @Description
 * @create 2019-02-19 23:04
 **/
public class LockTest {
    public static void main(String[] args) {
         final BooleanLock booleanLock = new BooleanLock();
        Stream.of("t1","t2","t3","t4").forEach(name->new Thread(()->{
            try {
                booleanLock.lock();
                Optional.of(Thread.currentThread().getName()+" have the lock Monitor").ifPresent(System.out::println);
                work();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }finally {
                booleanLock.unlock();
            }
        },name).start());
    }
    private static void work() throws InterruptedException{
        Optional.of(Thread.currentThread().getName()+" is working...").ifPresent(System.out::println);
        Thread.sleep(5_000);
    }
}
